import {StyleSheet, View} from "react-native";
import React, {useEffect, useState} from "react";
import {Modal, Portal, Text, Button, Provider, FAB} from 'react-native-paper';
import {Login} from "../Login/Login";
import {getStorage,removeStorage} from "../../util/storageUtil";
import {UserType} from "../../api/type";

function HomeScreen({ navigation }:{navigation?:any}) {
    const [visible, setVisible] = useState<boolean>(false);
    const [userDetail,setUserDetail] = useState<UserType>()
    useEffect(()=>{
        getStorage("userDetail").then((res)=>{
            if (res){
                setUserDetail(res)
            }else {
                showModal()
            }
        })
    },[])
    const [state, setState] = React.useState({ open: false });

    const onStateChange = ({ open }:any) => setState({ open });

    const { open } = state;

    const showModal = () => setVisible(true);
    const hideModal = () => setVisible(false);
    const containerStyle = {width:'100%',flex:1};
    return (
        <Provider>
            <Portal>
                <Modal visible={visible} onDismiss={hideModal} contentContainerStyle={containerStyle}>
                    <Login userDetail={userDetail} closeLogin={setVisible} />
                </Modal>
            </Portal>
            <FAB.Group
                open={open}
                visible
                icon={open ? 'allergy' :'alien'}
                actions={[
                    {
                        icon: 'plus',
                        onPress: () => console.log('Pressed add'),
                        testID: 'plus',
                    },
                    {
                        icon: 'account-circle',
                        label: '登录',
                        onPress: () => {showModal()},
                        testID: 'account-circle',
                    },
                    {
                        icon: 'wifi-cancel',
                        label: '清除缓存',
                        onPress: async () => {
                            await removeStorage("token")
                            await removeStorage("userDetail")
                        },
                        testID: 'cancel',
                    },
                    {
                        icon: 'airballoon',
                        label: '返回主页',
                        onPress: () => {
                            navigation?.navigate('WellComeOne')
                        },
                        testID: 'airballoon',
                    },
                ]}
                onStateChange={onStateChange}
                onPress={() => {
                    if (open) {
                        // do something if the speed dial is open
                    }
                }}
            ></FAB.Group>
        </Provider>
    );
}
const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
    },
});

export {HomeScreen}